/*

  Copyright (C) 2003-2009 Smardec. All rights reserved.

  http://www.smardec.com

*/

package com.smardec.license4j.demo;

import com.smardec.license4j.License;
import com.smardec.license4j.LicenseManager;

import java.util.Date;


/**
 * Shows how to create license.
 */
public class CreateNewLicense {
    /**
     * Creates license based on file with features.
     *
     * @param args
     */
    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Arguments: FeaturesFileName LicenseFileName");
            System.exit(0);
        }
       // createLicenseFromFileTemplate(args[0], args[1]);
        createLicenseFromFileTemplate("features.txt", "License_IO.lic");
     }

    /**
     * Creates license based on file with features.
     *
     * @param fileTemplate name of the file with features
     * @param licenseFile  resulting license file name
     */
    public static void createLicenseFromFileTemplate(String fileTemplate, String licenseFile) {
        try {
            // loading features from the specified file
            License license = LicenseManager.loadLicense(fileTemplate);
           // license.addFeature("Issued", new Date());
           // license.addUnsignedFeature("UnsignedFeature", "This text can be changed while the license will be still valid");
            // init private key. Private key is generated by CreateKeyPair class.
            //LicenseManager.setPrivateKey("3082014B0201003082012C06072A8648CE3804013082011F02818100FD7F53811D75122952DF4A9C2EECE4E7F611B7523CEF4400C31E3F80B6512669455D402251FB593D8D58FABFC5F5BA30F6CB9B556CD7813B801D346FF26660B76B9950A5A49F9FE8047B1022C24FBBA9D7FEB7C61BF83B57E7C6A8A6150F04FB83F6D3C51EC3023554135A169132F675F3AE2B61D72AEFF22203199DD14801C70215009760508F15230BCCB292B982A2EB840BF0581CF502818100F7E1A085D69B3DDECBBCAB5C36B857B97994AFBBFA3AEA82F9574C0B3D0782675159578EBAD4594FE67107108180B449167123E84C281613B7CF09328CC8A6E13C167A8B547C8D28E0A3AE1E2BB3A675916EA37F0BFA213562F1FB627A01243BCCA4F1BEA8519089A883DFE15AE59F06928B665E807B552564014C3BFECF492A04160214263FD73569FD1C7C7B0356CF45B09F59961D63F0");
            LicenseManager.setPrivateKey("3082014B0201003082012C06072A8648CE3804013082011F02818100FD7F53811D75122952DF4A9C2EECE4E7F611B7523CEF4400C31E3F80B6512669455D402251FB593D8D58FABFC5F5BA30F6CB9B556CD7813B801D346FF26660B76B9950A5A49F9FE8047B1022C24FBBA9D7FEB7C61BF83B57E7C6A8A6150F04FB83F6D3C51EC3023554135A169132F675F3AE2B61D72AEFF22203199DD14801C70215009760508F15230BCCB292B982A2EB840BF0581CF502818100F7E1A085D69B3DDECBBCAB5C36B857B97994AFBBFA3AEA82F9574C0B3D0782675159578EBAD4594FE67107108180B449167123E84C281613B7CF09328CC8A6E13C167A8B547C8D28E0A3AE1E2BB3A675916EA37F0BFA213562F1FB627A01243BCCA4F1BEA8519089A883DFE15AE59F06928B665E807B552564014C3BFECF492A041602143090ED9D7D48B7F3C906BE1D30D2A54B821B908D");
            LicenseManager.setPublicKey("308201B83082012C06072A8648CE3804013082011F02818100FD7F53811D75122952DF4A9C2EECE4E7F611B7523CEF4400C31E3F80B6512669455D402251FB593D8D58FABFC5F5BA30F6CB9B556CD7813B801D346FF26660B76B9950A5A49F9FE8047B1022C24FBBA9D7FEB7C61BF83B57E7C6A8A6150F04FB83F6D3C51EC3023554135A169132F675F3AE2B61D72AEFF22203199DD14801C70215009760508F15230BCCB292B982A2EB840BF0581CF502818100F7E1A085D69B3DDECBBCAB5C36B857B97994AFBBFA3AEA82F9574C0B3D0782675159578EBAD4594FE67107108180B449167123E84C281613B7CF09328CC8A6E13C167A8B547C8D28E0A3AE1E2BB3A675916EA37F0BFA213562F1FB627A01243BCCA4F1BEA8519089A883DFE15AE59F06928B665E807B552564014C3BFECF492A0381850002818100DEE60927A6ED9B6AC469D79BFFCDAE1992B2A3D6D4717F87EC6D0BDBA3085EE899990645BB7E33159C9EF1CF4C9A079BD1C413EF404C277E1470F096CF1E2C78FFFB7BF02F77B616A9D63AD4A865F1A98811FBC25AE89EB7AAE36B9512EE4C8BAD19A3C985647F41182829D020B9D83C10BBAE0729F8152C47F855DE919548A5");
            LicenseManager.saveLicense(license, licenseFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Creates license from scratch.
     */
    public static void createLicenseFromScratch() {
        try {
            // init private key. Private key is generated by CreateKeyPair class.
            LicenseManager.setPrivateKey("3082014B0201003082012C06072A8648CE3804013082011F02818100FD7F53811D75122952DF4A9C2EECE4E7F611B7523CEF4400C31E3F80B6512669455D402251FB593D8D58FABFC5F5BA30F6CB9B556CD7813B801D346FF26660B76B9950A5A49F9FE8047B1022C24FBBA9D7FEB7C61BF83B57E7C6A8A6150F04FB83F6D3C51EC3023554135A169132F675F3AE2B61D72AEFF22203199DD14801C70215009760508F15230BCCB292B982A2EB840BF0581CF502818100F7E1A085D69B3DDECBBCAB5C36B857B97994AFBBFA3AEA82F9574C0B3D0782675159578EBAD4594FE67107108180B449167123E84C281613B7CF09328CC8A6E13C167A8B547C8D28E0A3AE1E2BB3A675916EA37F0BFA213562F1FB627A01243BCCA4F1BEA8519089A883DFE15AE59F06928B665E807B552564014C3BFECF492A04160214263FD73569FD1C7C7B0356CF45B09F59961D63F0");
            License license = new License();
            // adding needed features
            license.addFeature("Customer", "Customer name");
            license.addFeature("Company", "Company name");
            license.addFeature("Feature1", "true");
            license.addFeature("Feature2", "false");
            license.addFeature("Expire", "10-10-2009");
            license.addFeature("Issued", new Date());
            license.addUnsignedFeature("UnsignedFeature", "This text can be changed while the license will be still valid");
            LicenseManager.saveLicense(license, "demo.lic");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
